package com.syxp.pda.service.plan.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.springframework.transaction.annotation.Transactional;

import com.syxp.pda.core.dao.BaseDao;
import com.syxp.pda.core.service.impl.BaseServiceImpl;
import com.syxp.pda.dao.base.BaseDutyRuleDao;
import com.syxp.pda.dao.base.CHKLineDao;
import com.syxp.pda.dao.base.CHKPlanDao;
import com.syxp.pda.dao.base.DefineIDZoneDao;
import com.syxp.pda.entity.base.CHKLine;
import com.syxp.pda.entity.base.CHKPlan;
import com.syxp.pda.entity.base.DefineIDZone;
import com.syxp.pda.entity.base.User;
import com.syxp.pda.service.plan.CHKPlanService;

/**
 * @version 1.0 2011/07/25
 * @author Light
 *
 */
@Transactional
public class CHKPlanServiceImpl extends BaseServiceImpl<CHKPlan> implements
		CHKPlanService {

	private CHKPlanDao chkPlanDao;
	private CHKLineDao chkLineDao;
	private DefineIDZoneDao defineIDZoneDao;
	private BaseDutyRuleDao baseDutyRuleDao;
	
	public CHKPlanServiceImpl(BaseDao<CHKPlan> baseDao) {
		super(baseDao);
	}

	@Override
	public List getPlans() {
		return chkPlanDao.getAll();
	}

	@Override
	public List getUnimportLines() {
		String sql = "select l.chklineid, l.linename from el_chkline l where l.chklineid not in (select distinct pl.chklineid from el_plan_line pl)";
		List list = this.chkPlanDao.getPlanDetail(sql);
		return list;
	}
	
	@Override
	public void importLines(int planId, List lineIdList) {
		CHKPlan plan = chkPlanDao.get(planId);
		Set<CHKLine> set = plan.getChkLines();
		for(Integer id: (List<Integer>)lineIdList) {
			set.add(this.chkLineDao.get(id));
		}
		chkPlanDao.update(plan);
	}

	@Override
	public void removeLine(int planId, int lineId) {
		CHKPlan plan = chkPlanDao.get(planId);
		Set<CHKLine> set = plan.getChkLines();
		CHKLine line = null;
		Iterator it = set.iterator();
		while(it.hasNext()) {
			line = (CHKLine)it.next();
			if(line.getChkLineID()==lineId) {
				break;
			}
			line = null;
		}
		set.remove(line);
		chkPlanDao.update(plan);
	}

	@Override
	public List<CHKLine> getLines(int planId) {
		List list = new ArrayList();
		list.addAll(this.getDao().get(planId).getChkLines());
		return list;
	}

	@Override
	public void setLineUsers(int lineId, List userList) {
		CHKLine line = this.chkLineDao.get(lineId);
		Set<User> set = line.getUsers();
		set.clear();
		chkLineDao.save(line);
		set.addAll(userList);
		chkLineDao.update(line);
		
	}
	
	@Override
	public List getUnimportZones() {
		String sql = "select z.zoneid, z.zonename from el_defineidzone z where z.zoneid not in (select distinct lz.zoneid from el_line_zone lz)";
		List list = this.chkPlanDao.getPlanDetail(sql);
		return list;
	}
	
	@Override
	public void importZone(int lineId, List zoneIdList) {
		CHKLine line = this.chkLineDao.get(lineId);
		Set<DefineIDZone> set = line.getDefineIDZones();
		for(Integer id: (List<Integer>)zoneIdList) {
			set.add(this.defineIDZoneDao.get(id));
		}
		chkLineDao.update(line);
		
	}

	@Override
	public void removeZone(int lineId, int zoneId) {
		CHKLine line = this.chkLineDao.get(lineId);
		Set<DefineIDZone> set = line.getDefineIDZones();
		DefineIDZone zone = null;
		Iterator it = set.iterator();
		while(it.hasNext()) {
			zone = (DefineIDZone)it.next();
			if(zone.getZoneID()==zoneId) {
				break;
			}
			zone = null;
		}
		set.remove(zone);
		chkLineDao.update(line);
		
	}

	@Override
	public List<DefineIDZone> getZones(int lineId) {
		List<DefineIDZone> list = new ArrayList<DefineIDZone> ();
		list.addAll(this.chkLineDao.get(lineId).getDefineIDZones());
		return list;
	}
	
	@Override
	public List getPlanDetail(int planId, int lineId, int zoneId, Date dutyDate) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String dateStr = sdf.format(dutyDate);
		String sql = "SELECT" +
				" EL_CHKPlan.CHKPlanID AS CHKPlanID, EL_CHKPlan.PlanName AS PlanName" +
				", EL_CHKLine.CHKLineID AS CHKLineID, EL_CHKLine.LineName AS LineName" +
				", EL_CHKLine.IsCHKOrder AS Line_IsCHKOrder, EL_DefineIDZone.ZoneID AS ZoneID" +
				", EL_DefineIDZone.ZoneName AS ZoneName, EL_DeviceBaseInfo.DeviceInfoID AS DeviceID" +
				", EL_DeviceBaseInfo.DeviceName AS DevName, EL_DeviceCheckStandard.CHKStandardID AS CHKStandardID" +
				", EL_DeviceCheckStandard.CHKPlace AS CHKPlace, EL_DeviceCheckStandard.Standard AS Standard" +
				", EL_CHKType.CHKTypeName AS CHKTypeName, EL_DeviceCheckStandard.CHKItem AS CHKItem, EL_ClassDuty.ClassID" +
				", EL_ClassDuty.ClaName AS ClaName, EL_Turn.TurnOrder AS TurnOrder, EL_Duty.DutyID AS DutyID, EL_Duty.DutyName AS DutyName" +
				", EL_User.UserName AS UserName" + 
				", (Convert(NVARCHAR(10), DateAdd(DAY, 0,'"+dateStr+"'), 120) + ' ' + Convert(NVARCHAR(8),EL_Turn.StartTime, 108))  AS Start_PDA_CHKDateTime" +
				", (Convert(NVARCHAR(10), DateAdd(DAY, DateDiff(DAY,EL_Turn.StartTime,EL_Turn.EndTime),'"+dateStr+"'), 120) + ' ' + Convert(NVARCHAR(8),EL_Turn.EndTime, 108))  AS End_PDA_CHKDateTime" +
				" FROM" +
				" EL_CHKPlan, EL_Plan_Line, EL_CHKLine, EL_Line_Zone, EL_DefineIDZone, EL_DeviceBaseInfo" +
				", EL_DeviceCheckStandard, EL_CHKType" +
				", EL_User,EL_Line_User, EL_DutyUser, EL_Duty, EL_Department" + 
				", EL_ClassDuty, EL_Turn, EL_BaseDutyRule, EL_DutyRule" + 
				" WHERE" +
				" (EL_CHKPlan.CHKPlanID = EL_Plan_Line.CHKPlanID) AND (EL_Plan_Line.CHKLineID = EL_CHKLine.CHKLineID)" + 
				" AND (EL_CHKLine.CHKLineID = EL_Line_Zone.CHKLineID) AND (EL_Line_Zone.ZoneID = EL_DefineIDZone.ZoneID)" + 
				" AND (EL_DefineIDZone.ZoneID = EL_DeviceBaseInfo.ZoneID) AND (EL_DeviceBaseInfo.DeviceInfoID = EL_DeviceCheckStandard.DeviceInfoID)" + 
				" AND (EL_DeviceCheckStandard.CHKTypeCode = EL_CHKType.CHKTypeCode)" + 
				" AND EL_CHKLine.CHKLineID=EL_Line_User.CHKLineID AND EL_Line_User.UserID=EL_User.UserID" + 
				" AND EL_User.UserID=EL_DutyUser.UserID AND EL_DutyUser.DutyID=EL_Duty.DutyID" + 
				" AND EL_Duty.DepartmentID=EL_Department.DepartmentID " + 
				" AND EL_Department.AutoID=EL_BaseDutyRule.AutoID AND EL_Department.DepartmentID=EL_DutyRule.DepartmentID" + 
				" AND EL_CHKLine.CHKLineID=EL_Turn.CHKLineID AND EL_Turn.ClassID=EL_ClassDuty.ClassID" + 
				" AND EL_DutyRule.DutyID=EL_Duty.DutyID AND EL_DutyRule.ClassID=EL_ClassDuty.ClassID" + 
				
				" AND (EL_DutyRule.DuRuOrder = DateDiff(DAY, EL_BaseDutyRule.BaseDate, '"+dateStr+"') % EL_BaseDutyRule.CircleDays + 1)" +
				" AND (EL_CHKPlan.CHKPlanID = "+planId+")" +
				" AND (EL_CHKLine.CHKLineID = "+lineId+") AND (EL_DefineIDZone.ZoneID = "+zoneId+")" +
				" ORDER BY" +
				" EL_Turn.StartTime ASC, EL_Turn.EndTime ASC, EL_CHKPlan.CHKPlanID ASC, EL_CHKLine.CHKLineID ASC" +
				", EL_DeviceCheckStandard.CHKStandardID ASC";
		List list = this.chkPlanDao.getPlanDetail(sql);		
		return list;
	}
	
	@Override
	public List download(Date beginDutyDate, Date endDutyDate, int dutyId, String username, String operatorcode) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String sqlProc = "{Call SP_EL_DownloadPlan("+dutyId+",'"+sdf.format(beginDutyDate)+
				"','"+sdf.format(endDutyDate)+"','"+username+"','"+operatorcode+"'"+")}";
		this.chkPlanDao.getPlanDetail(sqlProc);
		String sql = "select * from EL_CHKPlanDetail_Temp";
		return this.chkPlanDao.getPlanDetail(sql);
	}

	@Override
	public List queryPlan(Date dutyDate, int lineId, int dutyId,
			int classId) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String dateStr = sdf.format(dutyDate);
		StringBuffer sql = new StringBuffer("SELECT" +
				" EL_CHKPlan.CHKPlanID AS CHKPlanID, EL_CHKPlan.PlanName AS PlanName" + 
				", EL_CHKLine.CHKLineID AS CHKLineID, EL_CHKLine.LineName AS LineName" +
				", EL_CHKLine.IsCHKOrder AS Line_IsCHKOrder, EL_DefineIDZone.ZoneID AS ZoneID" +
				", EL_DefineIDZone.ZoneName AS ZoneName, EL_DeviceBaseInfo.DeviceInfoID AS DeviceID" +
				", EL_DeviceBaseInfo.DeviceName AS DevName, EL_DeviceCheckStandard.CHKStandardID AS CHKStandardID" +
				", EL_DeviceCheckStandard.CHKPlace AS CHKPlace, EL_DeviceCheckStandard.Standard AS Standard" +
				", EL_CHKType.CHKTypeName AS CHKTypeName, EL_DeviceCheckStandard.CHKItem AS CHKItem, EL_ClassDuty.ClassID" +
				", EL_ClassDuty.ClaName AS ClaName, EL_Turn.TurnOrder AS TurnOrder, EL_Duty.DutyID AS DutyID, EL_Duty.DutyName AS DutyName" +
				", EL_User.UserName AS UserName" + 
				", (Convert(NVARCHAR(10), DateAdd(DAY, 0,'"+dateStr+"'), 120) + ' ' + Convert(NVARCHAR(8),EL_Turn.StartTime, 108))  AS Start_PDA_CHKDateTime" +
				", (Convert(NVARCHAR(10), DateAdd(DAY, DateDiff(DAY,EL_Turn.StartTime,EL_Turn.EndTime),'"+dateStr+"'), 120) + ' ' + Convert(NVARCHAR(8),EL_Turn.EndTime, 108))  AS End_PDA_CHKDateTime" +
				" FROM" +
				" EL_CHKPlan, EL_Plan_Line, EL_CHKLine, EL_Line_Zone, EL_DefineIDZone, EL_DeviceBaseInfo" +
				", EL_DeviceCheckStandard, EL_CHKType" +
				", EL_User,EL_Line_User, EL_DutyUser, EL_Duty, EL_Department" + 
				", EL_ClassDuty, EL_Turn, EL_BaseDutyRule, EL_DutyRule" + 
				" WHERE" +
				" (EL_CHKPlan.CHKPlanID = EL_Plan_Line.CHKPlanID) AND (EL_Plan_Line.CHKLineID = EL_CHKLine.CHKLineID)" + 
				" AND (EL_CHKLine.CHKLineID = EL_Line_Zone.CHKLineID) AND (EL_Line_Zone.ZoneID = EL_DefineIDZone.ZoneID)" + 
				" AND (EL_DefineIDZone.ZoneID = EL_DeviceBaseInfo.ZoneID) AND (EL_DeviceBaseInfo.DeviceInfoID = EL_DeviceCheckStandard.DeviceInfoID)" + 
				" AND (EL_DeviceCheckStandard.CHKTypeCode = EL_CHKType.CHKTypeCode)" + 
				" AND EL_CHKLine.CHKLineID=EL_Line_User.CHKLineID AND EL_Line_User.UserID=EL_User.UserID" + 
				" AND EL_User.UserID=EL_DutyUser.UserID AND EL_DutyUser.DutyID=EL_Duty.DutyID" + 
				" AND EL_Duty.DepartmentID=EL_Department.DepartmentID " + 
				" AND EL_Department.AutoID=EL_BaseDutyRule.AutoID AND EL_Department.DepartmentID=EL_DutyRule.DepartmentID" + 
				" AND EL_CHKLine.CHKLineID=EL_Turn.CHKLineID AND EL_Turn.ClassID=EL_ClassDuty.ClassID" + 
				" AND EL_DutyRule.DutyID=EL_Duty.DutyID AND EL_DutyRule.ClassID=EL_ClassDuty.ClassID" + 
				

				" AND (EL_DutyRule.DuRuOrder = DateDiff(DAY, EL_BaseDutyRule.BaseDate, '"+dateStr+"') % EL_BaseDutyRule.CircleDays + 1)");

		
		if(lineId>=0) {
			sql.append(" AND (EL_CHKLine.CHKLineID = "+lineId+")");
		}
		if(dutyId>=0) {
			sql.append(" AND (EL_Duty.DutyID like '%"+dutyId+"%')");
		}
//		if(dutyId>=0) {
//			sql.append(" AND (EL_CHKPlan.TeamID like '%"+dutyId+"%')");
//		} else {
//			sql.append(" AND (EL_CHKPlan.TeamID like '%'+ltrim(str(EL_Duty.DutyID))+'%')");
//		}
		if(classId>=0) {
			sql.append(" AND (EL_ClassDuty.ClassID = "+classId+")");
		}
		sql.append(" ORDER BY" +
				" EL_Turn.StartTime ASC, EL_Turn.EndTime ASC, EL_CHKPlan.CHKPlanID ASC, EL_CHKLine.CHKLineID ASC" +
				", EL_DeviceCheckStandard.CHKStandardID ASC");
		
		List list = this.chkPlanDao.getPlanDetail(sql.toString());		
		return list;
	}

	/**
	 * @return the chkPlanDao
	 */
	public CHKPlanDao getChkPlanDao() {
		return chkPlanDao;
	}

	/**
	 * @param chkPlanDao the chkPlanDao to set
	 */
	public void setChkPlanDao(CHKPlanDao chkPlanDao) {
		this.chkPlanDao = chkPlanDao;
	}

	/**
	 * @return the chkLineDao
	 */
	public CHKLineDao getChkLineDao() {
		return chkLineDao;
	}

	/**
	 * @param chkLineDao the chkLineDao to set
	 */
	public void setChkLineDao(CHKLineDao chkLineDao) {
		this.chkLineDao = chkLineDao;
	}

	/**
	 * @return the defineIDZoneDao
	 */
	public DefineIDZoneDao getDefineIDZoneDao() {
		return defineIDZoneDao;
	}

	/**
	 * @param defineIDZoneDao the defineIDZoneDao to set
	 */
	public void setDefineIDZoneDao(DefineIDZoneDao defineIDZoneDao) {
		this.defineIDZoneDao = defineIDZoneDao;
	}
}
